/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.search.manualadd;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.groupme.controller.model.ImageSearchResult;
import net.groupme.controller.model.LocationSearchResult;
import net.groupme.controller.model.SearchResult;
import net.groupme.model.MODEL_CONSTANTS;

/**
 * created on 05.07.2007
 * 
 * This class is for adding a Resource manually.
 * 
 * @author Patrick Siehndel, <a
 *         href="mailto:patrick.siehndel@gmx.de">patrick.siehndel@gmx.de</a>
 * @author last edited by: $Author: mischa $
 * 
 * @version $Revision: 1.9 $ $Date: 2008-12-01 12:44:07 $
 */
public class ManualAddOfRessources {

	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());
	/* TODO: [HACK] -> Map-Search Engine */
	private static final String GOOGLE_MAPS_BASE = "maps.google";
	private static final String GOOGLE_MAPS_COORDINATES_PARAMETER = "ll=";
	
	/**
	 * Identifies the type of a manually given URL, sets parameters and returns the resource as SearchResult.
	 * @param url the url of the ressorce
	 * @param titel the titel of the ressorce
	 * @param description the description of the ressorce
	 * @return a List of {@link SearchResult}
	 */
	public List<SearchResult> addResourceManual(String urlString, String titel, String description){
		
		// Prüfen ob http:// vor dem String steht, wenn nicht hinzufügen
		if (!urlString.startsWith("http://")){
			urlString = "http://" + urlString;
		}
		URL url = null;
		try {
		url = new URL(urlString);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		List<SearchResult> resultList = new ArrayList<SearchResult>();
		
		//special YouTube protocol
		if (url.toString().contains("youtube.com") && url.toString().contains("watch?v=")){
			try {
				String vidId = url.toString().substring(url.toString().indexOf("v=")+2);
				if(vidId.indexOf("&") != -1){
					vidId = vidId.substring(0, vidId.indexOf("&"));
				}
				
				url = new URL("http://www.youtube.com/v/" + vidId);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		}
		//detect Type of content
		String contentType = "";
		try {
			contentType = url.openConnection().getContentType();
			logger.info("Detected Content-Type: " + contentType);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//image
		if (contentType != null && contentType.startsWith("image")){
			ImageSearchResult result = new ImageSearchResult();
			result.setUrl(url.toString());
			result.setType(MODEL_CONSTANTS.RESOURCE_TYPE_IMAGE);
			result.setDescription(description);
			result.setUrlOfThumbnail(url.toString());
			result.setName(titel);
			result.setResultID("result" + 0);
			resultList.add(result);
		}
		//video
		else if (contentType != null && contentType.startsWith("video")){
			SearchResult result = new SearchResult();
			result.setUrl(url.toString());
			result.setType(MODEL_CONSTANTS.RESOURCE_TYPE_VIDEO);
			result.setDescription(description);
			result.setName(titel);
			result.setResultID("result" + 0);
			resultList.add(result);
		}
		//audio
		else if (contentType != null && contentType.startsWith("audio")){
			SearchResult result = new SearchResult();
			result.setUrl(url.toString());
			result.setType(MODEL_CONSTANTS.RESOURCE_TYPE_AUDIO);
			result.setDescription(description);
			result.setName(titel);
			result.setResultID("result" + 0);
			resultList.add(result);
		}
		//application
		else if (contentType != null && contentType.startsWith("application/x-shockwave-flash")){
			SearchResult result = new SearchResult();
			result.setUrl(url.toString());
			result.setType(MODEL_CONSTANTS.RESOURCE_TYPE_SHOCKWAVE);
			result.setDescription(description);
			result.setName(titel);
			result.setResultID("result" + 0);
			resultList.add(result);
			
		}
		//rss [HACK]
		else if (contentType != null && (contentType.startsWith("text/xml")
				|| contentType.startsWith("application/rss+xml")
				|| contentType.startsWith("application/rdf+xml")
				|| contentType.startsWith("application/atom+xml"))){
			SearchResult result = new SearchResult();
			result.setUrl(url.toString());
			result.setType(MODEL_CONSTANTS.RESOURCE_TYPE_RSS);
			result.setDescription(description);
			result.setName(titel);
			result.setResultID("result" + 0);
			resultList.add(result);
			
		}
		//other
		else {
			//TODO: [HACK] google Maps
			if (urlString != null && urlString.contains(GOOGLE_MAPS_BASE)
					&& urlString.contains(GOOGLE_MAPS_COORDINATES_PARAMETER)) {
				try{
				String coordinates = urlString.substring(urlString
						.indexOf(GOOGLE_MAPS_COORDINATES_PARAMETER) + GOOGLE_MAPS_COORDINATES_PARAMETER.length());
				String latitude = coordinates.substring(0, coordinates
						.indexOf(","));
				String longitude = coordinates.substring(coordinates
						.indexOf(",")+1, coordinates.indexOf("&"));
				LocationSearchResult result = new LocationSearchResult();
				result.setUrl(url.toString());
				result.setType(MODEL_CONSTANTS.RESOURCE_TYPE_WEBSITE);
				result.setDescription(description);
				result.setName(titel);
				result.setResultID("result" + 0);
				result.setLatitude(latitude);
				result.setLongitude(longitude);
				resultList.add(result);
				}catch(Exception e){
//					normal web sites
					SearchResult result = new SearchResult();
					result.setUrl(url.toString());
					result.setType(MODEL_CONSTANTS.RESOURCE_TYPE_WEBSITE);
					result.setDescription(description);
					result.setName(titel);
					result.setResultID("result" + 0);
					resultList.add(result);
				}
			} else {
				//normal web sites
				SearchResult result = new SearchResult();
				result.setUrl(url.toString());
				result.setType(MODEL_CONSTANTS.RESOURCE_TYPE_WEBSITE);
				result.setDescription(description);
				result.setName(titel);
				result.setResultID("result" + 0);
				resultList.add(result);
			}
		}
		return resultList;
		}
}

